import { toast } from "./tip";
export function handleBackData(data) {
  if (data.code == 0) {
    return true;
  } else {
    toast(data.msg);
  }
}

// 倒计时
export function getTimer(time = 10, callback, end, during = 1000) {
  callback(time);
  let timer = setInterval(() => {
    time--;
    callback(time);
    if (time === 0) {
      clearInterval(timer);
      end();
    }
  }, during);
  return timer;
}

export function isScrollBottom(e, callback) {
  let offsetY = e.nativeEvent.contentOffset.y; //滑动距离
  let contentSizeHeight = e.nativeEvent.contentSize.height; //scrollView contentSize高度
  let oriageScrollHeight = e.nativeEvent.layoutMeasurement.height; //scrollView高度
  if (offsetY + oriageScrollHeight >= contentSizeHeight) {
    callback && callback();
  }
}

export function checkNumber(theObj) {
  var reg = /^\d+(?=\.{0,1}\d+$|$)/;
  if (reg.test(theObj)) {
    return true;
  }
  return false;
}

export function numberToQian(s) {
    //千分位显示
    //alert(s+"3");
    s=parseFloat(s);//去除首位输入的0，如002，小数不影响parseFloat字符串转数字
    //alert(s);
    s=s.toString();
    s = s.replace(/^(\d*)$/, "$1.");
    s = (s + "00000").replace(/(\d*\.\d\d\d\d\d)\d*/, "$1");
    s = s.replace(".", ",");
    var re = /(\d)(\d{3},)/;
    while (re.test(s)) {
      s = s.replace(re, "$1,$2");
    }
      
    s = s.replace(/,(\d\d\d\d\d)$/, ".$1");
    return s;
}